package com.zb.springcloud.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zb.springcloud.Untils.OpenStackHttpUtil;
import com.zb.springcloud.entities.CommonResult;
import com.zb.springcloud.entities.Payment;
import com.zb.springcloud.lb.LoadBalancer;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class OrderController {

    private static final String URL = "http://localhost:8001";
    private static final String URL_NAME = "CLOUD-PAYMENT-SERVICE";
    @Resource
    RestTemplate restTemplate;
    @Resource
    private DiscoveryClient discoveryClient;
    @Resource
 private LoadBalancer loadBalancer;
    @PostMapping("consumer/payment/create")
    public CommonResult<Payment> create(@RequestBody Payment payment) {

        return restTemplate.postForObject("http://"+URL_NAME+"/payment/create", payment, CommonResult.class);
    }

    @GetMapping("consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id ) {

        //return restTemplate.getForObject(URL+"/payment/get/"+id,  CommonResult.class);
     return restTemplate.getForObject("http://"+URL_NAME+"/payment/get/"+id,  CommonResult.class);
//    Map<String, Object> map = new HashMap<>();
//        map.put("id", 31);
//        int status = OpenStackHttpUtil.get( URL + "/payment/get/"+id, new HashMap<>(), map).getStatus();
//        String body = OpenStackHttpUtil.get( URL + "/payment/get/"+id, new HashMap<>(), map).body();
//        JSONObject jsonObject = JSON.parseObject(body).getJSONObject("data");
//        String ids = jsonObject.getString("id");
//        System.out.println(ids);
//        System.out.println(body);
//        System.out.println(status);
//        return  new CommonResult();
    }
    @GetMapping("consumer/paymentByEntity/get/{id}")
    public CommonResult<Payment> getPaymentByEntity(@PathVariable("id") Long id ){
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity("http://" + URL_NAME + "/payment/get/" + id, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful()) {
            System.out.println(entity);
            return entity.getBody();
        }else {
            return new CommonResult<>(444,"操作失败");
        }
    }

    @PostMapping("consumer/paymentByEntity/create")
    public CommonResult<Payment> createByEntity(@RequestBody Payment payment) {

        ResponseEntity<CommonResult> entity = restTemplate.postForEntity("http://" + URL_NAME + "/payment/create", payment, CommonResult.class);
        if (entity.getStatusCode().is2xxSuccessful()) {
            System.out.println(entity);
            return entity.getBody();
        }else {
            return new CommonResult<>(444,"操作失败");
        }
    }

    @GetMapping("consumer/payment/lb")
    public String getPaymentLb(){
        List<ServiceInstance> url_name = discoveryClient.getInstances("CLOUD-PAYMENT-SERVICE");
        if (url_name == null || url_name.size() <= 0){
            return null;
        }
        ServiceInstance instances = loadBalancer.instances(url_name);
        URI uri = instances.getUri();
        return restTemplate.getForObject(uri + "/payment/lb", String.class);
    }
    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin(){
       return restTemplate.getForObject("http://"+URL_NAME+"/payment/zipkin/", String.class);
    }
}
